package com.ushine.test.utils;

import com.ushine.test.constant.StringTypeEnum;
import org.springframework.stereotype.Component;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

/**
 * @Author: yls
 * @Date 2022/3/15 15:43
 * @Description: 随机密码生成类
 * @Version 1.0
 **/
@Component
public class PasswordUtils {

    /**
     * 小写字母
     */
    private static String[] lowercase = {
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    };
    /**
     * 大写字母
     */
    private static String[] capital = {
            "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
            "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /**
     * 数字
     */
    private static String[] number = {
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};

    /**
     * 特殊符号
     */
    private static String[] sign = {
            "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "`", "-", "=",
            "{", "}", "|", ":", "\"", "<", ">", "?",
            "[", "]", "\\", ";", "'", ",", ".", "/"};

    /**
     * 静态随机数
     */
    private static Random random = null;

    static {
        try {
            random = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取随机组合码
     *
     * @param num  位数
     * @param type 类型
     * @type <br>字符型 LETTER,
     * <br>大写字符型 CAPITAL,
     * <br>数字型 NUMBER,
     * <br>符号型 SIGN,
     * <br>大+小字符型 LETTER_CAPITAL,
     * <br>小字符+数字 型 LETTER_NUMBER,
     * <br>大+小字符+数字 型 LETTER_CAPITAL_NUMBER,
     * <br>大+小字符+数字+符号 型 LETTER_CAPITAL_NUMBER_SIGN
     */
    public  String getRandom(int num, StringTypeEnum type) {
        ArrayList<String> temp = new ArrayList<>();
        StringBuffer code = new StringBuffer();
        if (type == StringTypeEnum.LETTER) {
            temp.addAll(Arrays.asList(lowercase));
        } else if (type == StringTypeEnum.CAPITAL) {
            temp.addAll(Arrays.asList(capital));
        } else if (type == StringTypeEnum.NUMBER) {
            temp.addAll(Arrays.asList(number));
        } else if (type == StringTypeEnum.SIGN) {
            temp.addAll(Arrays.asList(sign));
        } else if (type == StringTypeEnum.LETTER_CAPITAL) {
            temp.addAll(Arrays.asList(lowercase));
            temp.addAll(Arrays.asList(capital));
        } else if (type == StringTypeEnum.LETTER_NUMBER) {
            temp.addAll(Arrays.asList(lowercase));
            temp.addAll(Arrays.asList(number));
        } else if (type == StringTypeEnum.LETTER_CAPITAL_NUMBER) {
            temp.addAll(Arrays.asList(lowercase));
            temp.addAll(Arrays.asList(capital));
            temp.addAll(Arrays.asList(number));
        } else if (type == StringTypeEnum.LETTER_CAPITAL_NUMBER_SIGN) {
            temp.addAll(Arrays.asList(lowercase));
            temp.addAll(Arrays.asList(capital));
            temp.addAll(Arrays.asList(number));
            temp.addAll(Arrays.asList(sign));
        }
        for (int i = 0; i < num; i++) {
            code.append(temp.get(random.nextInt(temp.size())));
        }
        return code.toString();
    }


}
